<?php 

class Paginator 
{

	 public  $num_on_page;
	 public  $func_name;
	 public  $num_current;
	 private $num_center;
	 private $num_left;
	 public  $row_on_one_page;
	 private $total_pages;
	 private $html_code;
	 
	 public $query;
	 
	 public function __construct($func_name, $row_on_one_page, $num_on_page, $num_current, $p_query, $total_pages='none')
	 {
		 $this->func_name=$func_name;
		 $this->row_on_one_page=$row_on_one_page;
		 $this->num_on_page=$num_on_page;
		 $this->num_current=$num_current;
		 $this->query=$p_query;
		 $this->total_pages($total_pages);		 
	 }
	 
	 private function left_part ()
	 {
		return floor($this->num_on_page/2);
	 
	 }
	 
	 public function total_pages ($total)
	 {
		 if ($total=='none')
		 {
			 $result=mysql_query($this->query);
			 $this->total_pages=ceil(mysql_num_rows($result)/$this->row_on_one_page);
		 }
		 else
		 {
			 $this->total_pages=ceil($total/$this->row_on_one_page);
		 }		 
	 }
	 
	 private function center()
	 {
		 return ceil($this->num_on_page/2);
	 
	 }
	 
	 public function getHTML()
	 {	
		 if ($this->total_pages<=1)
			 return;
		 else	
			 $this->html_code="<ul>Страницы: ";
		 
		 if ($this->num_current<=$this->center())
		 {
			 for ($i=1;$i<=$this->total_pages;$i++)
			 {
				 if ($i<=$this->num_on_page)
				 {
					 if ($i==$this->num_current)
						 $this->html_code=$this->html_code."<li class='faq_page_nav cur_page_faq'>$i</li>";
					 else
						 $this->html_code=$this->html_code. "<li><a class='faq_page_nav' href='#' onclick=\"$this->func_name('$i');return false;\" >$i</a></li>";
				 }	
				else 
				{
					  $this->html_code=$this->html_code. "<li>..</li>";
					  $N=$this->total_pages;
					  $this->html_code=$this->html_code. "<li><a class='faq_page_nav' href='#' onclick=\"$this->func_name('$N')return false;\" >$N</a></li>";
					  break;
				}
			 
			 } 
		 
		 }	
		 else 
		 {
			 $a=$this->num_current-$this->left_part ();
			 if ($this->num_current>($this->total_pages-$this->left_part ()))
			 $a=$this->total_pages-$this->num_on_page+1;
			 
			 $this->html_code=$this->html_code. "<li><a class='faq_page_nav' href='#' onclick=\"$this->func_name('1')\" >1</a></li>";
			 $this->html_code=$this->html_code. "<li>..</li>";
			 
			 for ($i=$a;$i<=$this->total_pages;$i++)
			 {
				 if ($i<=$a+($this->num_on_page-1))
				 {
					 if ($i==$this->num_current)
						 $this->html_code=$this->html_code. "<li class='faq_page_nav cur_page_faq'>$i</li>";
					 else
						 $this->html_code=$this->html_code. "<li><a class='faq_page_nav' href='#' onclick=\"$this->func_name('$i'); return false;\" >$i</a></li>";
				 }	
				 else 
				{
					  $this->html_code=$this->html_code. "<li>..</li>";
					  $N=$this->total_pages;
					  $this->html_code=$this->html_code. "<li><a class='faq_page_nav' href='#' onclick=\"$this->func_name('$N'); return false;\" >$N</a></li>";
					  break;
				}			 
			 }	 
		 }	
			
		$this->html_code=$this->html_code. "</ul>";	
		return $this->html_code;
	 }



}
?>